{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import pickle\n",
    "from torch.utils.data import DataLoader\n",
    "from utils.SnliDataSet import SnliDataSet\n",
    "\n",
    "worddict_dir=\"data\\\\worddict.txt\"\n",
    "data_train_id_dir=\"data\\\\train_data_id.pkl\"\n",
    "data_dev_id_dir=\"data\\\\dev_data_id.pkl\"\n",
    "embedding_matrix_dir=\"data\\\\embedding_matrix.pkl\"\n",
    "\n",
    "#超参数\n",
    "batch_size=1\n",
    "use_gpu=True\n",
    "\n",
    "device=torch.device(\"cuda:0\" if use_gpu else \"cpu\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "hidden_size=50\n",
    "dropout=0.5\n",
    "num_classes=3\n",
    "lr=0.0004"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 加载数据\n",
    "with open(data_train_id_dir,'rb') as f:\n",
    "    train_data=SnliDataSet(pickle.load(f))\n",
    "train_loader=DataLoader(train_data,batch_size=batch_size,shuffle=True)\n",
    "\n",
    "with open(data_dev_id_dir,'rb') as f:\n",
    "    dev_data=SnliDataSet(pickle.load(f))\n",
    "dev_loader=DataLoader(dev_data,batch_size=batch_size,shuffle=False)\n",
    "\n",
    "#加载embedding\n",
    "with open(embedding_matrix_dir,'rb') as f:\n",
    "    embeddings=torch.tensor(pickle.load(f),dtype=torch.float).to(device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from model.esim import ESIM\n",
    "model = ESIM(embeddings.shape[0],\n",
    "             embeddings.shape[1],\n",
    "             hidden_size,\n",
    "             embeddings=embeddings,\n",
    "             dropout=dropout,\n",
    "             num_classes=num_classes,\n",
    "             device=device).to(device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#准备训练\n",
    "criterion = torch.nn.CrossEntropyLoss()\n",
    "optimizer = torch.optim.Adam(model.parameters(), lr=lr)\n",
    "scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer,mode=\"max\",factor=0.5,patience=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#训练过程中的参数\n",
    "best_score=0.0\n",
    "start_epoch=1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python36",
   "language": "python",
   "name": "python36"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
